package com.lefeng.cockpit.datax.service.impl;

import com.alibaba.fastjson.JSON;
import com.lefeng.cockpit.common.bean.dto.dataxjson.DataXJsonBuildDto;
import com.lefeng.cockpit.common.bean.po.datasource.DataSourcePO;
import com.lefeng.cockpit.datax.dao.DataSourceDao;
import com.lefeng.cockpit.datax.service.JobService;
import com.lefeng.cockpit.datax.tool.datax.DataxJsonHelper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * @author weifw_in
 */
@Service
@Transactional(readOnly = true)
@Slf4j
public class JobServiceImpl implements JobService {

    @Resource
    private DataSourceDao dataSourceDao;


    @Override
    public String buildJobJson(DataXJsonBuildDto dto) {
        DataxJsonHelper dataxJsonHelper = new DataxJsonHelper();
        // reader
        DataSourcePO readerDatasource = dataSourceDao.getById(dto.getReaderDatasourceId());
        // reader plugin init
        dataxJsonHelper.initReader(dto, readerDatasource);
        DataSourcePO writerDatasource = dataSourceDao.getById(dto.getWriterDatasourceId());
        dataxJsonHelper.initWriter(dto, writerDatasource);

        return JSON.toJSONString(dataxJsonHelper.buildJob());
    }
}